package com.example.minimusicdemo;

import java.util.ArrayList;

import com.example.project22_editingmediaplayer.LayoutAllSong;
import com.example.project22_editingmediaplayer.MainActivity;
import com.example.project22_editingmediaplayer.R;

import com.example.project22_editingmediaplayer.action.AudioUltility;
import com.example.project22_editingmediaplayer.adapter.RowSong;
import com.example.project22_editingmediaplayer.adapter.RowSongplayallsong;
import com.example.project22_editingmediaplayer.adapter.RowSongplaylistF;
import com.example.project22_editingmediaplayer.objects.SongObject;
import com.example.project22_editingmediaplayer.resource.SongResource;

import android.annotation.SuppressLint;
import android.content.Context;
import android.graphics.Color;
import android.os.AsyncTask;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.Toast;
import android.widget.AdapterView.OnItemClickListener;

public class Fragment_playlist extends Fragment {
	public static ListView lv_playlist;
	public static RowSongplaylistF ad_playlist;
	private ArrayList<SongObject> listSong_play;
	private SongObject song_play;
	private AudioUltility audioUltility;
	Context mplaylist_Context;
	private int indexSong;
	private boolean isService = false;
	ImageView imglastplay2;
	View viewlastplay1;

	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {

		View rootView = inflater.inflate(R.layout.fragment_playlist, container,
				false);
		setHasOptionsMenu(true);
		mplaylist_Context = container.getContext();
		lv_playlist = (ListView) rootView.findViewById(R.id.lvplay_list);
		audioUltility = new AudioUltility(mplaylist_Context);

		listSong_play = SongResource.listSong;
		ad_playlist = new RowSongplaylistF(listSong_play, mplaylist_Context);
		lv_playlist.setAdapter(ad_playlist);
		ad_playlist.notifyDataSetChanged();

		lv_playlist.setOnItemClickListener(onItemClick);

		return rootView;
	}

	OnItemClickListener onItemClick = new AdapterView.OnItemClickListener() {

		@Override
		public void onItemClick(AdapterView<?> arg0, View arg1, int position,
				long arg3) {

			MainActivity.mService.PlaySong(position);

			SongResource.listSong.get(MainActivity.mService.getlastIndex())
					.set_isplay(false);
			SongResource.listSong.get(MainActivity.mService.getIndex())
					.set_isplay(true);
			Fragment_playlist.ad_playlist.notifyDataSetChanged();

			setinformation(position);

		}
	};

	@Override
	public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
		// TODO Auto-generated method stub
		super.onCreateOptionsMenu(menu, inflater);
		menu.clear();
		inflater.inflate(R.menu.menuplaylist, menu);
	}

	public void setinformation(int position) {
		Fragment_playmain.updateProgressBar();

		Fragment_playmain.audio_player_track.setText(MainActivity.mService
				.getsong().get_name());
		Fragment_playmain.audio_player_album_artist
				.setText(MainActivity.mService.getsong().get_artist());
		Fragment_playmain.button_mainplay
				.setImageResource(R.drawable.apollo_holo_light_pause);
		MainActivity.btnPlay
				.setImageResource(R.drawable.apollo_holo_light_pause);
		MainActivity.namesong.setText(SongResource.listSong.get(position)
				.get_name());
		MainActivity.singer.setText(SongResource.listSong.get(position)
				.get_artist());

	}

}